package com.warrantyhotel.android.adapter;

import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

import com.warrantyhotel.android.R;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.WarrantiesTable;
import com.warrantyhotel.android.ui.WarrantyApplication;
import com.warrantyhotel.android.widget.CustomImageView;

public class WarrantyAdapter extends CursorAdapter {

	private LayoutInflater mInflater;
	private WarrantyApplication app;

	public WarrantyAdapter(Context context, Cursor c) {
		super(context, c, false);
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		app = (WarrantyApplication) context.getApplicationContext();
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		final WarrantyItemViewHolder listItem = (WarrantyItemViewHolder) view.getTag();
		final String warrantyText = getCursor().getString(WarrantyQuery.LIST_LABEL);
		final String imageUrl = getCursor().getString(WarrantyQuery.IMAGE_URL);
		listItem.txtPurchaseAndStrone.setText(warrantyText);

		if (null != imageUrl && !TextUtils.isEmpty(imageUrl)) {
			app.getImageLoaderNew().DisplayImage(imageUrl, listItem.imageItem);
		} else {
			listItem.imageItem.setImageResource(R.drawable.newsphoto_placeholder);
		}

	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		View view = mInflater.inflate(R.layout.warranty_item, null);
		final WarrantyItemViewHolder listItem = new WarrantyItemViewHolder();
		listItem.txtPurchaseAndStrone = (TextView) view.findViewById(R.id.item_title);
		listItem.imageItem = (CustomImageView) view.findViewById(R.id.item_image_warranty);

		view.setTag(listItem);
		return view;

	}

	public class WarrantyItemViewHolder {
		TextView txtPurchaseAndStrone;
		CustomImageView imageItem;

	}

	interface WarrantyQuery {
		final String[] projectionWarranty = { WarrantiesTable._ID, WarrantiesTable.WARRANTY_ID, WarrantiesTable.PRODUCT_PRICE, WarrantiesTable.LIST_LABEL, WarrantiesTable.IMAGE_URL };
		int ID = 0;
		int WARRANTY_ID = 1;
		int PRODUCT_PRICE = 2;
		int LIST_LABEL = 3;
		int IMAGE_URL = 4;
	}

}
